/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.dao.ReplyDao;
import com.qdu.entity.Reply;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ice
 */
public class ReplyDaoImpl extends BaseDaoImpl1 implements ReplyDao{

    @Override
    public int addReply(Reply r) {
        return executeUpdate("insert into Reply values(?,?,?,?,?)", r.getId(),r.getRid(),r.getContents(),r.getCid(),r.getTime());
    }

    @Override
    public int deleteReply(Reply r) {
        return executeUpdate("delete Comment where id=? and Rid=? and Cid=?", r.getId(),r.getRid(),r.getCid());
    }

    @Override
    public List<Object[]> getReplyListByCid(int cid) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Object[]> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select [User].id,[User].Uname,[User].Uicon,Reply.Rid,Reply.contents,Reply.Cid,Reply.[time] from Reply,[User] where Cid=? and Reply.id=[User].id");
            ps.setInt(1, cid);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Object[]{rs.getInt(1), rs.getString(2), rs.getString(3),rs.getInt(4),rs.getString(5),rs.getInt(6),rs.getDate(7)});
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }



    
}
